
#include "build.h"
#include "fig_gamecenter.h"
#include "fig_vars.h"
#include "fig_trophie_notification.h"
#include "fig_award_tracker.h"

#include "util/mmgr.h"		    // Needs to come in early
#include "util/common_macros.h"		// macros
#include "util/log.h"		    // logging
#include "util/timer.h"		    // logging


const std::string FIGGameCenter::LEADER_BOARD_NAME = "grp.ac_leaderboard";

FIGGameCenter&  FIGGameCenter::Instance()
{
	static FIGGameCenter	Inst;
	return Inst;
}


std::string		FIGGameCenter::GetLeaderBoardName()
{
	return LEADER_BOARD_NAME;
}

FIGGameCenter::FIGGameCenter()
{
    
}	
    
void            FIGGameCenter::OnGainAchievement(int ID)
{
	//int C = GetCompletedAchievementCount();
	//if( C == 8)
	//{
	//	UpdateValue((int)GOD_OF_ALIENS,true,true);
	//}
}
    
void            FIGGameCenter::OnSuccessfulySubmitted(int ID)
{

}
	
std::string		FIGGameCenter::MapTypeToName(int Type)
{
	switch ((ACHIEVEMENT_TYPE)Type)
	{
		
		case BOSS_KILLER:
			return "boss_killer";

		case VERTERN:
			return "vertern";

		case SUPER_PILOT:
			return "super_pilot";

		case ALIEN_CRUSHER:
			return "alien_crusher";

		case SENIOR_ALIEN_CRUSHER:
			return "senior_alien_crusher";

		case ALIEN_CRUSHER_ULTIMATE:
			return "alien_crusher_the_ultimate";

		case ZEALOUS_PILOT:
			return "zealous_pilot";

		case STARS_COLLECTOR:
			return "stars_collector";

		case GOD_OF_ALIENS:
			return "god";

	}

	return "unknown";
}

std::string		FIGGameCenter::GetMedalExplanation(int Type)
{
	switch ((ACHIEVEMENT_TYPE)Type)
	{
		

		case BOSS_KILLER:
			return "LOC_INFO_BOSS_KILLER";

		case VERTERN:
			return "LOC_INFO_VERTERN";

		case SUPER_PILOT:
			return "LOC_INFO_SUPER_PILOT";

		case ALIEN_CRUSHER:
			return "LOC_INFO_ALIEN_CRUSHER";

		case SENIOR_ALIEN_CRUSHER:
			return "LOC_INFO_SENIOR_ALIEN_CRUSHER";

		case ALIEN_CRUSHER_ULTIMATE:
			return "LOC_INFO_CRUSHER_ULTIMATE";

		case ZEALOUS_PILOT:
			return "LOC_INFO_ZEALOUS_PILOT";

		case STARS_COLLECTOR:
			return "Collected 500 Stars";

		case GOD_OF_ALIENS:
			return "Collected 1,000 Stars";

	}

	return "";
}


std::string			FIGGameCenter::GetString(int Type)
{
	switch ((ACHIEVEMENT_TYPE)Type)
	{
		
		case BOSS_KILLER:
			return "LOC_TITLE_BOSS_KILLER";

		case VERTERN:
			return "LOC_TITLE_VERTERN";

		case SUPER_PILOT:
			return "LOC_TITLE_SUPER_PILOT";

		case ALIEN_CRUSHER:
			return "LOC_TITLE_ALIEN_CRUSHER";

		case SENIOR_ALIEN_CRUSHER:
			return "LOC_TITLE_SENIOR_ALIEN_CRUSHER";

		case ALIEN_CRUSHER_ULTIMATE:
			return "LOC_TITLE_CRUSHER_ULTIMATE";

		case ZEALOUS_PILOT:
			return "LOC_TITLE_ZEALOUS_PILOT";

		case STARS_COLLECTOR:
			return "LOC_TITLE_STARS_COLLECTOR";

		case GOD_OF_ALIENS:
			return "LOC_TITLE_GOD";

	}

	return "";	
}

void		FIGGameCenter::OnRetrieveGainedAchivements()
{

	// todo: check last player name.
	// if it is same, send data to gamecenter
	// if not, reset game RMS with data from gamecenter when it is loaded. and change the last player name
	
	// synchronize achievements

	for( int i = 0; i < ACHIEVEMENT_C; i++)
	{
		if( i == (int)ACHIEVEMENT_NONE)
			continue;

		int IsAlreadyGained = FIGVars::Instance()->GetInt((int)FIGVars::VAR_NAMES_ACHIEVEMENT_1 + i - 1  );

		if(IsAlreadyGained == 1)
		{
			// resubmit if it is not submit yet.
			// the gamecenter cache will handle duplication

			this->UpdateValue( i , true, true);
		}
	}
}

void        FIGGameCenter::UpdateValue(int Type,bool Success,bool IsFinalDecision)
{
	// resubmit if it is not submit yet.
	// the gamecenter cache will handle duplication
	//CGameCenterManager::UpdateValue(Type,Success,IsFinalDecision);

	//	offline cache
	if(IsFinalDecision)
	{
		int IsAlreadyGained = FIGVars::Instance()->GetInt((int)FIGVars::VAR_NAMES_ACHIEVEMENT_1 + Type - 1 );
		
		if(IsAlreadyGained == 0)
		{
			FIGTrophieNotification::Instance().ShowNotification(
																GetString(Type), 
																GetMedalImageName(Type),
																Type
																);
			FIGAwardTracker::Instance().GotAchievement(Type);
		}

		FIGVars::Instance()->SetInt((int)FIGVars::VAR_NAMES_ACHIEVEMENT_1 + Type - 1, 1  );
		
	}
	
}


std::string		FIGGameCenter::GetMedalImageName(int Type)
{
	switch ((ACHIEVEMENT_TYPE)Type)
	{
		
		case ALIEN_CRUSHER:
			return "alien_crusher";

		case ALIEN_CRUSHER_ULTIMATE:
			return "alien_crusher_ultimate";

		case SENIOR_ALIEN_CRUSHER:
			return "senior_alien_crusher";

		//-----------------------

		case STARS_COLLECTOR:
			return "stars_collector";

		case ZEALOUS_PILOT:
			return "zealous_pilot";    		

		case GOD_OF_ALIENS:
			return "god_of_aliens";

		//---------------------------

		case BOSS_KILLER:
			return "boss_killer";

		case VERTERN:
			return "veteran";

		case SUPER_PILOT:
			return "super_pilot";

		//--------------------------

		default:
			break;

	}

	return "";
}


//void		FIGGameCenter::Internal_LoginOnSuccess(const std::wstring& PlayerName)
//{
	// todo: check last player name.
	// if it is same, send data to gamecenter
	// if not, reset game RMS with data from gamecenter when it is loaded. and change the last player name
//
//}

//void		FIGGameCenter::Internal_LoginOnError()
//{
//
//}



